package com.sxfq.loansupermarket.backsystem.modular.marketing.controller;

import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.common.persistence.model.User;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.DateTimeKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.modular.arrival.entity.CustomerInfo;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.TcInfo;
import com.sxfq.loansupermarket.backsystem.modular.doctorLooked.entity.*;
import com.sxfq.loansupermarket.backsystem.modular.tcCenter.entity.ConsultantInfo;
import com.sxfq.loansupermarket.backsystem.modular.tcCenter.entity.DepartmentInfo;
import com.sxfq.loansupermarket.backsystem.modular.websocket.WebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 医生看诊/update
 *
 * @author py
 * @Date 2018年4月19日23:15:54
 */
@Controller
@RequestMapping("/reservationTcView")
public class ReservationTcViewController extends BaseController {

    private String PREFIX = "/doctorLooked/";
    private String PREFIXS = "/arrival/customer/";

    @Autowired
    private SqlMapper sqlMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    WebSocket webSocket;

    /**
     * 跳转到导医新增tc预约
     */
    @RequestMapping("/doctor_addTaskTcMarket/{eventData}")
    public String doctor_addTaskTcMarket(@PathVariable String eventData, Model model) {
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Abbreviation",0)+"";
        String startTime = eventData.split(",")[0];
        String endTime = eventData.split(",")[1];
        String tcId= eventData.split(",")[2];
        String continued = eventData.split(",")[3];
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        if (ShiroKit.getUser().getTcList() == null) {
            String getListSql = " select a.id as id,a.account as tcName from user a left join  dept b on a.deptid like CONCAT('%',b.id,',','%') left join role c on a.roleid=c.id " +
                    " where b.abbreviation='"+tableHead+"' and c.name like CONCAT('%','tc','%') ";
            List<TcInfo> tcList =sqlMapper.selectList(getListSql,TcInfo.class);
            ShiroKit.getUser().setTcList(tcList);
        }
        if (redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"jysList",0)== null) {
            String sql = " select a.id as id,a.account as doctor from user a left join  dept b on a.deptid like CONCAT('%',b.id,',','%') left join role c on find_in_set(c.id,a.roleid) > 0 " +
                    " where b.abbreviation='" + tableHead + "' and c.name='洁牙师' and b.simplename= '" + ShiroKit.getUser().getOutpatientName() + "'";
            redisTemplate.opsForList().leftPush(ShiroKit.getUser().getAccount()+"jysList", sqlMapper.selectList(sql));
        }
        if (ShiroKit.getUser().getDepartmentList() == null) {
            String getListSql = "select a.department as department from " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"pay_service_grade_info",0) + " a where a.is_upper=1 group by a.department order by a.id;";
            List<DepartmentInfo> departmentList=sqlMapper.selectList(getListSql,DepartmentInfo.class);
            ShiroKit.getUser().setDepartmentList(departmentList);
        }
        String sqltc = " select a.id as id,a.account as tcName from user a where id ='"+tcId+"'";
        TcInfo tcInfo=sqlMapper.selectOne(sqltc,TcInfo.class);
        model.addAttribute("departmentList",ShiroKit.getUser().getDepartmentList());
        model.addAttribute("jysList", redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"jysList",0));
        model.addAttribute("tcList", ShiroKit.getUser().getTcList());
        model.addAttribute("startTime", startTime);
        model.addAttribute("endTime", endTime);
        model.addAttribute("tcName", tcInfo.getTcName());
        model.addAttribute("continued", continued);
        return PREFIX + "doctor_addTaskTcMarket.html";
    }

    /**
     * 导医-tc视图 获取所有上班tc
     */
    @RequestMapping(value = "/getTcViewListed/{timeSlot}")
    @ResponseBody
    public List<ConsultantInfo> getTcViewListed(@PathVariable String timeSlot) {
        String startTime = timeSlot.split(",")[0].substring(0, 10);
        String endTime = timeSlot.split(",")[1].substring(0, 10);
        String sqlc=" ";
        if("Tc".equals(redisTemplate.opsForList().index(ShiroKit.getUser().getAccount() + "RoleName", 0))){
            sqlc=" and a.account='"+ShiroKit.getUser().getAccount()+"'";
        }
        String sql = "select a.id as id,a.account as tcName from user a \n" +
                " left join role b on a.roleid like CONCAT('%',b.id,'%')\n" +
                " where b.name like '%tc%'  " +sqlc+
                " and a.deptid like CONCAT('%'," + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"DeptId",0) + ",',','%') and a.status=1 " +
                " and a.id not in(select b.tc_id from tc_scheduling b " +
                " where b.the_date>='" + startTime + "' and b.the_date<='" + endTime + "' and b.status=2 " +
                " and b.outpatient_name='" + ShiroKit.getUser().getOutpatientName() + "')";
        List<ConsultantInfo> todaytcList = sqlMapper.selectList(sql, ConsultantInfo.class);
        return todaytcList;
    }

    /**
     * 市场-tc视图 获取所有上班tc 隐藏
     */
    @RequestMapping(value = "/getRMTcViewListed/{timeSlot}")
    @ResponseBody
    public List<ConsultantInfo> getRMTcViewListed(@PathVariable String timeSlot) {
        String startTime = timeSlot.split(",")[0].substring(0, 10);
        String endTime = timeSlot.split(",")[1].substring(0, 10);
        String sql = "select a.id as id,'TC' as tcName from user a \n" +
                " left join role b on a.roleid like CONCAT('%',b.id,'%')\n" +
                " where b.name like '%tc%'  " +
                " and a.deptid like CONCAT('%'," + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"DeptId",0) + ",',','%') and a.status=1 " +
                " and a.id not in(select b.tc_id from tc_scheduling b " +
                " where b.the_date>='" + startTime + "' and b.the_date<='" + endTime + "' and b.status=2 " +
                " and b.outpatient_name='" + ShiroKit.getUser().getOutpatientName() + "')";
        List<ConsultantInfo> todaytcList = sqlMapper.selectList(sql, ConsultantInfo.class);
        return todaytcList;
    }

    /**
     * 查找导医预约的tc患者
     */
    @RequestMapping(value = "/getDataTcList/{timeSlot}")
    @ResponseBody
    public List<ConsultantInfo> getDataTcList(@PathVariable String timeSlot) {
        String startTime = timeSlot.split(",")[0].substring(0, 10);
        String endTime = timeSlot.split(",")[1].substring(0, 10);
        String startTime2 = timeSlot.split(",")[0];
        String endTime2 = timeSlot.split(",")[1];
        String sql1 = "";
        if (startTime.equals(endTime)) {
            sql1 += "left(t.start_time,10) = '" + startTime + "'";
        } else {
            sql1 += "t.start_time >= '" + startTime2 + "' and t.end_time <= '" + endTime2 + "'";
        }
        String sql = " select * from (" +
                " select t1.id as id,t.customer_name as customerName,t.customer_status as customerStatus,t.check_status as checkStatus,t.customer_age as customerAge,t.customer_phone as customerPhone,t.medical_record_number as medicalRecordNumber, (  CASE t.customer_sex  WHEN 1 THEN      '男'  WHEN 2 THEN      '女'  ELSE      '未知'  END     ) as sex ,t.customer_sex_head as customerSexHead,t.archiving as archiving ,t.reservations_id as reservationsId,t.yellow_number as yellowNumber,t.treatment_unit as treatmentUnit,t.account_arrears as accountArrears,(select count(id) from " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"customer_info",0) + " b where t.reservations_id=b.reservations_id and left(start_time,10)=left(now(),10)) as todaySum,(select count(id) from " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"customer_info",0) + " b where t.reservations_id=b.reservations_id and left(start_time,10)>left(now(),10)) as aftersSum,(select count(id) from " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"customer_info",0) + " b where t.reservations_id=b.reservations_id and left(start_time,10)<left(now(),10)) as beforeSum,t.arrival_status as arrivalStatus,t.arrival_store as arrivalStore ,t.birthday as birthday ,left(t.create_appointment_time,19) as createAppointmentTime,ifnull(t.remarks,'') as remarks,ifnull(t.remarks_reservation,'') as remarksReservation, t.field_tc as fieldTc,t.first_visit_date as firstVisitDate,t.appointment_pay_service as appointmentPayService ,t.start_time as startTime, t.end_time as endTime,t.doctor,t.id as customerId \n" +
                " from " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"customer_info",0) + " t \n" +
                " left join user t1 on t.field_tc=t1.account \n" +
                " where "+sql1+" and t.arrival_status!=8 and t.isMarketTc=1 \n" +
                " and t.reservations_id not in (select reservations_id from " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"customer_info",0) + " t2 " +
                " where t.reservations_id=t2.reservations_id and left(t.start_time,10)=left(t2.start_time,10) and status='到店')\n" +
                " union all\n" +
                " select t1.id as id,t.customer_name as customerName,t.customer_status as customerStatus,t.check_status as checkStatus,t.customer_age as customerAge,t.customer_phone as customerPhone,t.medical_record_number as medicalRecordNumber, (  CASE t.customer_sex  WHEN 1 THEN      '男'  WHEN 2 THEN      '女'  ELSE      '未知'  END     ) as sex ,t.customer_sex_head as customerSexHead,t.archiving as archiving ,t.reservations_id as reservationsId,t.yellow_number as yellowNumber,t.treatment_unit as treatmentUnit,t.account_arrears as accountArrears,(select count(id) from " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"customer_info",0) + " b where t.reservations_id=b.reservations_id and left(start_time,10)=left(now(),10)) as todaySum,(select count(id) from " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"customer_info",0) + " b where t.reservations_id=b.reservations_id and left(start_time,10)>left(now(),10)) as aftersSum,(select count(id) from " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"customer_info",0) + " b where t.reservations_id=b.reservations_id and left(start_time,10)<left(now(),10)) as beforeSum,t.arrival_status as arrivalStatus,t.arrival_store as arrivalStore ,t.birthday as birthday ,left(t.create_appointment_time,19) as createAppointmentTime,ifnull(t.remarks,'') as remarks,ifnull(t.remarks_reservation,'') as remarksReservation, t.field_tc as fieldTc,t.first_visit_date as firstVisitDate,t.appointment_pay_service as appointmentPayService ,t.start_time as startTime, t.end_time as endTime,t.doctor,t.id as customerId \n" +
                " from " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"customer_info",0) + " t \n" +
                " left join user t1 on t.field_tc=t1.account \n" +
                " where "+sql1+" and t.arrival_status!=8 and t.status='到店'  \n" +
                " and t.reservations_id in (select reservations_id from " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"customer_info",0) + " t2 " +
                " where t.reservations_id=t2.reservations_id and left(t.start_time,10)=left(t2.start_time,10) and isMarketTc=1 ) " +
                " ) t2 order by t2.startTime " ;
        List<ConsultantInfo> DataList = sqlMapper.selectList(sql, ConsultantInfo.class);
        return DataList;
    }

    /**
     * 当前拉取tc名下患者获取
     */
    @RequestMapping(value = "/getDataListByTc/{timeSlot}")
    @ResponseBody
    public List<ConsultantInfo> getDataListByTc(@PathVariable String timeSlot) {
        String startTime = timeSlot.split(",")[0].substring(0, 11);
        String tcName = timeSlot.split(",")[2];
        //根据角色用户查询所有医生
        String sql = " select t1.id as id,t.customer_name as customerName,t.customer_age as customerAge,customer_status as customerStatus,t.customer_phone as customerPhone,t.medical_record_number as medicalRecordNumber," +
                " ( " +
                " CASE t.customer_sex " +
                " WHEN 1 THEN " +
                "     '男' " +
                " WHEN 2 THEN " +
                "     '女' " +
                " ELSE " +
                "     '未知' " +
                " END " +
                "    ) as sex,t.archiving as archiving ,t.reservations_id as reservationsId,t.arrival_status as arrivalStatus ," +
                " t.arrival_store as arrivalStore ,t.birthday as birthday ,left(t.create_appointment_time,19) as createAppointmentTime," +
                " t.field_tc as fieldTc,t.first_visit_date as firstVisitDate,t.appointment_pay_service as appointmentPayService ," +
                " t.start_time as startTime, t.end_time as endTime,t.field_tc as tcName,t.id as customerId,t.account_arrears as accountarrears," +
                " ifnull(t.remarks,'') as remarks,ifnull(t.remarks_reservation,'') as remarksReservation " +
                " from " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"customer_info",0) + " t " +
                " left join user t1 on t.field_tc=t1.account where left(t.start_time,10) = '" + startTime.substring(0, 10) + "' " +
                " and field_tc='" + tcName + "' and t.arrival_status!=8 ";
        List<ConsultantInfo> DataList = sqlMapper.selectList(sql, ConsultantInfo.class);
        return DataList;
    }

    /**
     * 获取TC当前的休息状态
     */
    @RequestMapping(value = "/getTcStatus", method = RequestMethod.POST)
    @ResponseBody
    public String getTcStatus(String tcName, String value) throws Exception {
        String sql = "select t.status from tc_scheduling t where t.tc_name='" + tcName + "' and left(t.the_date,10)='" + value.substring(0, 10) + "' and t.outpatient_name='" + ShiroKit.getUser().getOutpatientName() + "' limit 1";
        DoctorLookedInfo doctorLookedInfo = sqlMapper.selectOne(sql, DoctorLookedInfo.class);
        if (doctorLookedInfo == null) {
            return "1";
        } else {
            return doctorLookedInfo.getStatus();
        }
    }

    //tc预约视图拖拽修改
    @RequestMapping(value = "/updateViewsTc/{cri}")
    @ResponseBody
    public int updateViewsTc(@PathVariable String cri) {
        String startTime = cri.split(",")[0];
        String endTime = cri.split(",")[1];
        String tcId = cri.split(",")[2];
        String customerId = cri.split(",")[3];
        String tc = "select account from user where id='" + tcId + "' limit 1";
        User user = sqlMapper.selectOne(tc, User.class);
        String sql = " update " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"customer_info",0) + " set start_time='" + startTime + "'," +
                "end_time='"+ endTime + "',field_tc=(select account from user where id='" + tcId + "')," +
                "query_tc=(select account from user where id='" + tcId + "') where id='" + customerId + "'";
        String sql2 = "select t.customer_name as customerName,t.customer_phone as customerPhone," +
                " t.start_time as startTime,t.end_time as endTime,t.field_tc as fieldTc,t.doctor as doctor,t.reservations_id as reservationsId from " +
                " " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"customer_info",0) + " t where id='" + customerId + "' limit 1";
        CustomerInfo customerInfo = sqlMapper.selectOne(sql2, CustomerInfo.class);
        //插入视图改动日志 修改时间or医生
        String sq11 = "INSERT INTO `customer_reservations_update_log` (createtime,single_person,logtype, `userid`, `customer_status`, `customer_name`,`customer_phone`," +
                " `succeed`,outpatient_name ,`message`,customer_id,reservations_id) VALUES " +
                " ('" + DateTimeKit.now() + "','" + ShiroKit.getUser().getAccount() + "','修改预约','" + ShiroKit.getUser().getId() + "','预约', '"
                + customerInfo.getCustomerName() + "', '" + customerInfo.getCustomerPhone() + "','成功','" + ShiroKit.getUser().getOutpatientName() +
                "','预约时间：" + customerInfo.getStartTime().substring(0, 16) + " - " + customerInfo.getEndTime().substring(11, 16)
                + " -> " + startTime.substring(0, 16) + " - " + endTime.substring(11, 16) + "" +
                " </br> TC：" + customerInfo.getFieldTc() + " -> " + user.getAccount() + "','" + customerId + "','" + customerInfo.getReservationsId() + "');";
        sqlMapper.update(sql);
        webSocket.sendMessageViews("修改预约", redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Abbreviation",0)+"");
        return sqlMapper.insert(sq11);
    }

    /**
     * 点tc名字跳转到tc对应周视图
     */
    @RequestMapping("/customer_tcWeek/{ids}")
    public String customer_tcWeek(@PathVariable String ids, Model model) {
        String id = ids.split(",")[0];
        String time = ids.split(",")[1];
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Abbreviation",0)+"";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        String sql1 = " select t.tc_id as tcId ,t1.account as tcName ,t.the_date as theDate" +
                " from tc_scheduling t  left join user t1 on t.tc_id=t1.id " +
                " where t.tc_id='" + id + "' and t.status=2 " +
                " and t.the_date between '" + time + "'-INTERVAL WEEKDAY('" + time + "') DAY and '" + time + "'+ INTERVAL 6-WEEKDAY('" + time + "') DAY ";
        List<DoctorSchedulingInfo> schedulingInfo = sqlMapper.selectList(sql1, DoctorSchedulingInfo.class);
        String sql = "select * from user where id ='" + id + "'";
        User user = sqlMapper.selectOne(sql, User.class);
        model.addAttribute("id", id);
        model.addAttribute("time", time);
        model.addAttribute("tcName", user.getAccount());
        model.addAttribute("schedulingInfo", schedulingInfo);
        return PREFIXS + "/customer_tcWeek.html";
    }

    /**
     * 点tc名字跳转到tc对应周视图
     */
    @RequestMapping("/customer_RMtcWeek/{ids}")
    public String customer_RMtcWeek(@PathVariable String ids, Model model) {
        String id = ids.split(",")[0];
        String time = ids.split(",")[1];
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Abbreviation",0)+"";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        String sql1 = " select t.tc_id as tcId ,t1.account as tcName ,t.the_date as theDate" +
                " from tc_scheduling t  left join user t1 on t.tc_id=t1.id " +
                " where t.tc_id='" + id + "' and t.status=2 " +
                " and t.the_date between '" + time + "'-INTERVAL WEEKDAY('" + time + "') DAY and '" + time + "'+ INTERVAL 6-WEEKDAY('" + time + "') DAY ";
        List<DoctorSchedulingInfo> schedulingInfo = sqlMapper.selectList(sql1, DoctorSchedulingInfo.class);
        String sql = "select * from user where id ='" + id + "'";
        User user = sqlMapper.selectOne(sql, User.class);
        model.addAttribute("id", id);
        model.addAttribute("time", time);
        model.addAttribute("tcName", user.getAccount());
        model.addAttribute("schedulingInfo", schedulingInfo);
        return PREFIXS + "/customer_RMtcWeek.html";
    }

    /**
     * 当前个人tc视图查找数据源  传开始时间和结束时间(一周时间)  tcID
     */
    @RequestMapping(value = "/getDataTc/{timeSlot}")
    @ResponseBody
    public List<ConsultantInfo> getDataTc(@PathVariable String timeSlot) {
        String startTime = timeSlot.split(",")[0].substring(0, 11);
        String endTime = timeSlot.split(",")[1].substring(0, 11);
        String tcId = timeSlot.split(",")[2];
        String startTime2 = timeSlot.split(",")[0];
        String endTime2 = timeSlot.split(",")[1];
        String sql1 = "";
        if (startTime.equals(endTime)) {
            sql1 += "t.start_time  like CONCAT('%" + startTime + "%')";
        } else {
            sql1 += "t.start_time >=  '" + startTime2 + "' and t.end_time <= '" + endTime2 + "'";
        }
        String sql2 = "select id,account from user where id ='" + tcId + "' limit 1";
        User user = sqlMapper.selectOne(sql2, User.class);
        String sql = " select * from (" +
                " select t1.id as id,t.customer_name as customerName,t.customer_status as customerStatus,t.check_status as checkStatus,t.customer_age as customerAge,t.customer_phone as customerPhone,t.medical_record_number as medicalRecordNumber, (  CASE t.customer_sex  WHEN 1 THEN      '男'  WHEN 2 THEN      '女'  ELSE      '未知'  END     ) as sex ,t.customer_sex_head as customerSexHead,t.archiving as archiving ,t.reservations_id as reservationsId,t.yellow_number as yellowNumber,t.treatment_unit as treatmentUnit,t.account_arrears as accountArrears,(select count(id) from " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"customer_info",0) + " b where t.reservations_id=b.reservations_id and left(start_time,10)=left(now(),10)) as todaySum,(select count(id) from " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"customer_info",0) + " b where t.reservations_id=b.reservations_id and left(start_time,10)>left(now(),10)) as aftersSum,(select count(id) from " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"customer_info",0) + " b where t.reservations_id=b.reservations_id and left(start_time,10)<left(now(),10)) as beforeSum,t.arrival_status as arrivalStatus,t.arrival_store as arrivalStore ,t.birthday as birthday ,left(t.create_appointment_time,19) as createAppointmentTime,ifnull(t.remarks,'') as remarks,ifnull(t.remarks_reservation,'') as remarksReservation, t.field_tc as fieldTc,t.first_visit_date as firstVisitDate,t.appointment_pay_service as appointmentPayService ,t.start_time as startTime, t.end_time as endTime,t.doctor,t.id as customerId \n" +
                " from " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"customer_info",0) + " t \n" +
                " left join user t1 on t.field_tc=t1.account \n" +
                " where "+sql1+" and t.arrival_status!=8 and field_tc='" + user.getAccount() + "' and t.isMarketTc=1 \n" +
                " and t.reservations_id not in (select reservations_id from " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"customer_info",0) + " t2 " +
                " where t.reservations_id=t2.reservations_id and left(t.start_time,10)=left(t2.start_time,10) and status='到店')\n" +
                " union all\n" +
                " select t1.id as id,t.customer_name as customerName,t.customer_status as customerStatus,t.check_status as checkStatus,t.customer_age as customerAge,t.customer_phone as customerPhone,t.medical_record_number as medicalRecordNumber, (  CASE t.customer_sex  WHEN 1 THEN      '男'  WHEN 2 THEN      '女'  ELSE      '未知'  END     ) as sex ,t.customer_sex_head as customerSexHead,t.archiving as archiving ,t.reservations_id as reservationsId,t.yellow_number as yellowNumber,t.treatment_unit as treatmentUnit,t.account_arrears as accountArrears,(select count(id) from " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"customer_info",0) + " b where t.reservations_id=b.reservations_id and left(start_time,10)=left(now(),10)) as todaySum,(select count(id) from " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"customer_info",0) + " b where t.reservations_id=b.reservations_id and left(start_time,10)>left(now(),10)) as aftersSum,(select count(id) from " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"customer_info",0) + " b where t.reservations_id=b.reservations_id and left(start_time,10)<left(now(),10)) as beforeSum,t.arrival_status as arrivalStatus,t.arrival_store as arrivalStore ,t.birthday as birthday ,left(t.create_appointment_time,19) as createAppointmentTime,ifnull(t.remarks,'') as remarks,ifnull(t.remarks_reservation,'') as remarksReservation, t.field_tc as fieldTc,t.first_visit_date as firstVisitDate,t.appointment_pay_service as appointmentPayService ,t.start_time as startTime, t.end_time as endTime,t.doctor,t.id as customerId \n" +
                " from " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"customer_info",0) + " t \n" +
                " left join user t1 on t.field_tc=t1.account \n" +
                " where "+sql1+" and t.arrival_status!=8 and field_tc='" + user.getAccount() + "' and t.status='到店'  \n" +
                " and t.reservations_id in (select reservations_id from " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"customer_info",0) + " t2 " +
                " where t.reservations_id=t2.reservations_id and left(t.start_time,10)=left(t2.start_time,10) and isMarketTc=1 ) " +
                " ) t2 order by t2.startTime " ;
        List<ConsultantInfo> DataWeekList = sqlMapper.selectList(sql, ConsultantInfo.class);
        return DataWeekList;
    }

    /**
     * 获得此tc本周休息的日期
     */
    @RequestMapping(value = "/getTcWeekSchedul/{timeSlot}", method = RequestMethod.POST)
    @ResponseBody
    public List<DoctorSchedulingInfo> getTcWeekSchedul(@PathVariable String timeSlot, Model model) {
        String startTime = timeSlot.split(",")[0];
        String endTime = timeSlot.split(",")[1];
        String tcId = timeSlot.split(",")[2];
        String sql1 = " select t.tc_id as tcId ,t1.account as tcName ,t.the_date as theDate" +
                " from tc_scheduling t left join user t1 on t.tc_id=t1.id " +
                " where t.tc_id='" + tcId + "' and t.status=2 " +
                " and t.the_date between '" + startTime + "' and '" + endTime + "' and t.outpatient_name='" + ShiroKit.getUser().getOutpatientName() + "' ";
        List<DoctorSchedulingInfo> schedulingInfo = sqlMapper.selectList(sql1, DoctorSchedulingInfo.class);
        return schedulingInfo;
    }

    //市场点击确定
    @RequestMapping(value = "/sureAppointmentSC", method = RequestMethod.POST)
    @ResponseBody
    public int sureAppointmentSC(String customerId) {
        String selsql = "select * from  " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"customer_info",0) + " where id = '" + customerId + "'";
        CustomerInfo customerIn = sqlMapper.selectOne(selsql, CustomerInfo.class);
        String sql = " ";
        if (("9").equals(customerIn.getArrivalStatus()) || ("10").equals(customerIn.getArrivalStatus())) { //新诊市场点击确定
            sql = "update " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"customer_info",0)+ "  set arrival_status ='10' where id = '" + customerId + "'";
        } else {
            sql = "update " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"customer_info",0) + "  set arrival_status ='6' where id = '" + customerId + "'";
        }
        //插入视图改动日志 预约确定
        String sq11 = "INSERT INTO `customer_reservations_update_log` (createtime,single_person,logtype, `userid`, `customer_status`, `customer_name`,`customer_phone`," +
                " `succeed`,outpatient_name ,`message`,customer_id,reservations_id) VALUES " +
                " ('" + DateTimeKit.now() + "','" + ShiroKit.getUser().getAccount() + "','市场点击确定','" + ShiroKit.getUser().getId() + "','预约', '"
                + customerIn.getCustomerName() + "', '" + customerIn.getCustomerPhone() + "','成功','" + ShiroKit.getUser().getOutpatientName() +
                "','预约状态：预约->确定','" + customerId + "','" + customerIn.getReservationsId() + "');";
        sqlMapper.insert(sq11);
        webSocket.sendMessageViews("确定", ShiroKit.getUser().getAbbreviation());
        return sqlMapper.update(sql);
    }

    //导医点击确定
    @RequestMapping(value = "/sureAppointmentDY", method = RequestMethod.POST)
    @ResponseBody
    public int sureAppointmentDY(String customerId) {
        String selsql = "select * from  " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"customer_info",0)+ " where id = '" + customerId + "'";
        CustomerInfo customerIn = sqlMapper.selectOne(selsql, CustomerInfo.class);
        String sql = " ";
        if (("9").equals(customerIn.getArrivalStatus()) || ("10").equals(customerIn.getArrivalStatus())) {  //新诊导医点击确定
            sql = "update " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"customer_info",0) + "  " +
                    "set arrival_status ='13',guide_doctor='"+ShiroKit.getUser().getAccount()+"' where id = '" + customerId + "'";
        } else {
            sql = "update " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"customer_info",0)+ "  " +
                    "set arrival_status ='14',guide_doctor='"+ShiroKit.getUser().getAccount()+"' where id = '" + customerId + "'";
        }
        //插入视图改动日志 预约确定
        String sq11 = "INSERT INTO `customer_reservations_update_log` (createtime,single_person,logtype, `userid`, `customer_status`, `customer_name`,`customer_phone`," +
                " `succeed`,outpatient_name ,`message`,customer_id,reservations_id) VALUES " +
                " ('" + DateTimeKit.now() + "','" + ShiroKit.getUser().getAccount() + "','导医点击确定','" + ShiroKit.getUser().getId() + "','预约', '"
                + customerIn.getCustomerName() + "', '" + customerIn.getCustomerPhone() + "','成功','" + ShiroKit.getUser().getOutpatientName() +
                "','预约状态：预约->确定','" + customerId + "','" + customerIn.getReservationsId() + "');";
        sqlMapper.insert(sq11);
        webSocket.sendMessageViews("确定", ShiroKit.getUser().getAbbreviation());
        return sqlMapper.update(sql);
    }
}